package com.sihai.ehr.web.action.rank;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.Grade;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.GradeService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.List;

/**
 * @author wxn
 * @since 2012-6-7 20:08
 * 职等设置
 */
@Results({
        @Result(name = "list", location = "grade_list.jsp"),
        @Result(name = "add", location = "grade_add.jsp"),
        @Result(name = "main", location = "rank_main.jsp")
})
public class GradeAction extends EHRAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672240978L;

    @BeanInject
    private GradeService gradeService;
    @BeanInject
    private DutyService dutyService;

    private LimitedList<Grade> grades;

    private Long currentDutyId;
    private List<Duty> duties;

    private Grade grade;

    private String gradeIds;

    @Action("/rank/grade")
    @Override
    public String execute() throws Exception {
        listAction = "/rank/grade_list.xhtml";
        listTitle = "职等设置";
        return "main";
    }

    @Action("/rank/grade_list")
    public String list() throws Exception {
        //得到当前组织结构的职务列表
        duties = dutyService.search(organizationId, null, null);

        //得到当前职务的职等列表
        if(currentDutyId == null || currentDutyId <= 0){
            if(duties != null && !duties.isEmpty()){
                currentDutyId = duties.get(0).getId();
            }else{
                return "list";
            }
        }
        Grade g = new Grade();
        Duty d = new Duty();
        d.setId(currentDutyId);
        g.setDuty(d);
        grades = gradeService.search(g, createLimit(), createSorter());

        /* 分页 */
        this.paging(grades);
        /* 设置翻页action */
        this.setPageUrl("rank/grade_list.xhtml");
        
        return "list";
    }

    @Action("/rank/grade_create")
    public String create() throws Exception {
        if(grade == null){
            return execute();
        }

        if(grade.getId() != null && grade.getId() > 0){//edit
            Grade g = gradeService.get(grade.getId());
            g.setGradeName(grade.getGradeName());
            g.setGradeSort(grade.getGradeSort());
            g.setGradePay(grade.getGradePay());
            g.setGradeContent(grade.getGradeContent());
            g.setPerformance(grade.getPerformance());
            g.setSalaryStructure(grade.getSalaryStructure());	//modify by henry
            gradeService.save(g);
        }else{ //save
            gradeService.save(grade);
        }

        return execute();
    }

    //跳转到添加/编辑页面
    @Action("/rank/grade_add")
    public String add() throws Exception {
        //edit
        if(grade != null && grade.getId() != null){
            grade = gradeService.get(grade.getId());
        }

        return  "add";
    }

    @Action("/rank/grade_delete")
    public String remove() throws Exception {
        if(StringUtils.isBlank(gradeIds)){
            return execute();
        }
        gradeService.remove(gradeIds);
        return execute();
    }

    public LimitedList<Grade> getGrades() {
        return grades;
    }

    public void setGrades(LimitedList<Grade> grades) {
        this.grades = grades;
    }

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }

    public String getGradeIds() {
        return gradeIds;
    }

    public void setGradeIds(String gradeIds) {
        this.gradeIds = gradeIds;
    }

    public Long getCurrentDutyId() {
        return currentDutyId;
    }

    public void setCurrentDutyId(Long currentDutyId) {
        this.currentDutyId = currentDutyId;
    }

    public List<Duty> getDuties() {
        return duties;
    }

    public void setDuties(List<Duty> duties) {
        this.duties = duties;
    }
}
